package com.yougou.ui.search;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yougou.R;
import com.yougou.base.BaseActivity;
import com.yougou.ui.category.adapter.CategoryGoodsAdapter;
import com.yougou.utils.ToastUtils;

public class SearchActivity extends BaseActivity {
    
    public static final String EXTRA_SEARCH_QUERY = "extra_search_query";
    
    private EditText mSearchInput;
    private ImageView mBtnBack;
    private ImageView mBtnSearch;
    private RecyclerView mRecyclerView;
    private View mEmptyLayout;
    private TextView mTvEmptyText;
    private View mProgressBar;
    private CategoryGoodsAdapter mAdapter;
    private SearchViewModel mViewModel;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        initData();
    }
    
    @Override
    protected void initView() {
        mSearchInput = findViewById(R.id.et_search);
        mBtnBack = findViewById(R.id.iv_back);
        mBtnSearch = findViewById(R.id.iv_search);
        mRecyclerView = findViewById(R.id.rv_search_result);
        mEmptyLayout = findViewById(R.id.layout_empty);
        mTvEmptyText = findViewById(R.id.tv_empty_text);
        mProgressBar = findViewById(R.id.progress_bar);
        
        // 初始化列表
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        mAdapter = new CategoryGoodsAdapter();
        mRecyclerView.setAdapter(mAdapter);
        
        // 返回按钮
        mBtnBack.setOnClickListener(v -> finish());
        
        // 搜索按钮
        mBtnSearch.setOnClickListener(v -> performSearch());
        
        // 输入监听
        mSearchInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    mBtnSearch.setVisibility(View.VISIBLE);
                } else {
                    mBtnSearch.setVisibility(View.GONE);
                }
            }
        });
    }
    
    @Override
    protected void initData() {
        mViewModel = new ViewModelProvider(this).get(SearchViewModel.class);
        
        // 观察搜索结果
        mViewModel.getSearchResult().observe(this, goods -> {
            mProgressBar.setVisibility(View.GONE);
            if (goods != null && !goods.isEmpty()) {
                mAdapter.setData(goods);
                showResult();
            } else {
                mTvEmptyText.setText("未找到相关商品");
                showEmpty();
            }
        });
        
        // 观察加载状态
        mViewModel.isLoading().observe(this, loading -> {
            if (loading != null) {
                mProgressBar.setVisibility(loading ? View.VISIBLE : View.GONE);
                if (loading) {
                    mEmptyLayout.setVisibility(View.GONE);
                    mRecyclerView.setVisibility(View.GONE);
                }
            }
        });
        
        // 观察错误
        mViewModel.getError().observe(this, error -> {
            mProgressBar.setVisibility(View.GONE);
            if (error != null) {
                ToastUtils.show(this, error);
                mTvEmptyText.setText(error);
                showEmpty();
            }
        });
        
        // 设置商品点击事件
        mAdapter.setOnItemClickListener(goods -> {
            // 跳转到商品详情页
            android.content.Intent intent = new android.content.Intent(this, com.yougou.ui.product.ProductDetailActivity.class);
            intent.putExtra(com.yougou.ui.product.ProductDetailActivity.EXTRA_GOODS_ID, goods.getGoodsId());
            startActivity(intent);
        });
        
        // 检查是否从其他页面传入搜索关键词
        String searchQuery = getIntent().getStringExtra(EXTRA_SEARCH_QUERY);
        if (searchQuery != null && !searchQuery.isEmpty()) {
            mSearchInput.setText(searchQuery);
            mViewModel.search(searchQuery);
        } else {
            // 显示初始空状态
            showEmpty();
        }
    }
    
    
    private void showEmpty() {
        mEmptyLayout.setVisibility(View.VISIBLE);
        mRecyclerView.setVisibility(View.GONE);
    }
    
    
    private void showResult() {
        mEmptyLayout.setVisibility(View.GONE);
        mRecyclerView.setVisibility(View.VISIBLE);
    }
    
    
    private void performSearch() {
        String keyword = mSearchInput.getText().toString().trim();
        if (keyword.isEmpty()) {
            ToastUtils.show(this, "请输入搜索关键词");
            return;
        }
        mViewModel.search(keyword);
    }
}

